﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Collections.Concurrent;
using System.Data.Odbc;

namespace QuanLyQuangCao
{
    public partial class frmDangKy_View : Form
    {
        public static frmDangKy_View View;
        //public Connection layDuLieu = new Connection();      

        public string bButton = "";
        public SqlDataAdapter da = new SqlDataAdapter();
        public DataSet ds = new DataSet();
        public DataTable dt = new DataTable();   
        public BindingSource bds = new BindingSource();

        public frmDangKy_Edit frmDangKy_Edit = new frmDangKy_Edit();
        

        public frmDangKy_View()
        {
            View = this;
            InitializeComponent();
        }
        SqlConnection Conn = new SqlConnection(C_Conn.Connection);
        public void frmDangKy_View_Load(object sender, EventArgs e)
        {
            Conn.Open();
            string chuoi = @"SELECT Ma_Dang_Ky AS [Mã Đăng Ký], Ma_Khach AS [Mã Khách], Ma_loai AS [Mã Loại], Gia_Mot_Ky AS [Giá Một Kỳ], So_Ky_Dang AS [Số Kỳ Đăng], Ngay_Bat_Dau AS [Ngày Bắt Đầu], Ma_Nhan_Vien AS [Mã Nhân Viên] FROM DangKyQuangCao";
          
            da = new SqlDataAdapter(chuoi, Conn);            
            da.Fill(ds, "DangKyQuangCao");
            dt = ds.Tables["DangKyQuangCao"];
            //Sua lai ten cot trong dgv
            //GetColumn(dt);

            bds.DataSource = dt;
            dgvDangKy_View.DataSource = bds;
            
            //
            dgvDangKy_View.ReadOnly = true;
            dgvDangKy_View.Columns[0].Visible = false;         
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            bButton = "Them";                
            frmDangKy_Edit frm = new frmDangKy_Edit();
            frm.ShowDialog();           
        }        
        public void Update(BindingSource bds)
        {
            dgvDangKy_View.DataSource = bds;            
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            bButton = "Sua";

            int Row = Convert.ToInt32(dgvDangKy_View.CurrentRow.Index);

            frmDangKy_Edit.txtMa_Dang_Ky.Text = dgvDangKy_View.Rows[Row].Cells[0].Value.ToString();
            frmDangKy_Edit.cboMa_Khach.Text = dgvDangKy_View.Rows[Row].Cells[1].Value.ToString();
            frmDangKy_Edit.cboMa_Loai.Text = dgvDangKy_View.Rows[Row].Cells[2].Value.ToString();
            frmDangKy_Edit.cboGia_Mot_Ky.Text = dgvDangKy_View.Rows[Row].Cells[3].Value.ToString();
            frmDangKy_Edit.txtSo_Ky_Dang.Text = dgvDangKy_View.Rows[Row].Cells[4].Value.ToString();
            frmDangKy_Edit.dteNgay_Bat_Dau.Text = dgvDangKy_View.Rows[Row].Cells[5].Value.ToString();
            frmDangKy_Edit.cboMa_Nhan_Vien.Text = dgvDangKy_View.Rows[Row].Cells[6].Value.ToString();

            frmDangKy_Edit.ShowDialog();
        }                       
        private void btXoa_Click(object sender, EventArgs e)
        {
            string sqlXoa = @"DELETE FROM DangKyQuangCao WHERE Ma_Dang_Ky = @Ma_Dang_Ky";
            SqlCommand cmdXoa = new SqlCommand(sqlXoa, Conn);
            cmdXoa.Parameters.Add("@Ma_Dang_Ky", SqlDbType.Int);
            cmdXoa.Parameters["@Ma_Dang_Ky"].Value = dgvDangKy_View.CurrentRow.Cells[0].Value;
            cmdXoa.ExecuteNonQuery();

            //Gan lai source cho dvg
            dt.Clear();
            string chuoi = @"SELECT Ma_Dang_Ky AS [Mã Đăng Ký], Ma_Khach AS [Mã Khách], Ma_loai AS [Mã Loại], Gia_Mot_Ky AS [Giá Một Kỳ], So_Ky_Dang AS [Số Kỳ Đăng], Ngay_Bat_Dau AS [Ngày Bắt Đầu], Ma_Nhan_Vien AS [Mã Nhân Viên] FROM DangKyQuangCao";
            da = new SqlDataAdapter(chuoi, Conn);
            da.Fill(dt);
            bds.DataSource = dt;
            
            dgvDangKy_View.DataSource = bds;            
            MessageBox.Show("Đã thực hiện xong", "Thông báo", MessageBoxButtons.OK);                
        }
        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();   
        }        
    }
}
